package net.expectx.gw.aliyun;


import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.ObjectMetadata;
import net.expectx.gw.util.AdminConstants;
import org.apache.commons.lang.StringUtils;
import sun.misc.BASE64Decoder;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

public class AliyunOSSUtil {

    /**
     * 阿里云存储切入点
     */
    private static final String ossEndPort= "oss-cn-shanghai.aliyuncs.com";
    /**
     * 阿里云存储密钥
     */
    private static final String ossAccessKeyId = "pqJGy4Jz4zCnbD6s";
    /**
     * 阿里云存储安全密钥
     */
    private static final String ossAccessKeySercet= "59AMu3zC0FsxKzriuNU4JIrEJU5RpX";
    /**
     * 阿里云存储文件夹
     */
    private static final String ossBuckName= "chiship";
    public static Map<String,Object> upload(String root,String catalog, String imageName, InputStream in){
        Map<String,Object>map=new HashMap<String, Object>();
        Boolean flag=false;
        String msg="";
        try {
            String fileUrl=null;
            OSSClient client = new OSSClient(ossEndPort, ossAccessKeyId,ossAccessKeySercet);
            if (StringUtils.isNotEmpty(catalog)){
                fileUrl="http://"+ossBuckName+"."+ossEndPort+"/"+root+"/"+catalog+"/"+imageName;
                client.putObject(ossBuckName, root+"/"+catalog+"/"+imageName, in);
            }else {
                fileUrl="http://"+ossBuckName+"."+ossEndPort+"/"+root+"/"+imageName;
                client.putObject(ossBuckName, root+"/"+imageName, in);
            }
            flag=true;
            map.put("fileUrl",fileUrl);
            client.shutdown();
        }catch (Exception e){
            msg="SmsUtil发生错误:"+e.getLocalizedMessage();
            System.out.println("SmsUtil发生错误:"+e.getLocalizedMessage());
        }

        map.put("flag",flag);
        map.put("msg",msg);
        return  map;
    }

    public static Map<String,Object> uploadBase64(String root,String catalog, String imageName,String base64){
        Map<String,Object>map=new HashMap<String, Object>();
        Boolean flag=false;
        String msg="";
        try {
            String fileUrl=null;

            OSSClient client = new OSSClient(ossEndPort, ossAccessKeyId,ossAccessKeySercet);
            byte[] bytes = new BASE64Decoder().decodeBuffer(base64);
            InputStream inputStream = new ByteArrayInputStream(bytes);
            ObjectMetadata metadata = new ObjectMetadata();
            metadata.setContentLength(inputStream.available());
            if (StringUtils.isNotEmpty(catalog)){
                fileUrl="http://"+ossBuckName+"."+ossEndPort+"/"+root+"/"+catalog+"/"+imageName;
                client.putObject(ossBuckName, root+"/"+catalog+"/"+imageName, inputStream);
            }else {
                fileUrl="http://"+ossBuckName+"."+ossEndPort+"/"+root+"/"+imageName;
                client.putObject(ossBuckName, root+"/"+imageName, inputStream);
            }
           flag=true;
            map.put("fileUrl",fileUrl);
            client.shutdown();
        }catch (Exception e){
            msg="SmsUtil发生错误:"+e.getLocalizedMessage();
            System.out.println("SmsUtil发生错误:"+e.getLocalizedMessage());
        }

        map.put("flag",flag);
        map.put("msg",msg);
        return  map;
    }

}
